package com.itbaizhan.openhis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbaizhan.openhis.domain.Producer;
import com.itbaizhan.openhis.dto.ProducerDto;
import com.itbaizhan.openhis.mapper.ProducerMapper;
import com.itbaizhan.openhis.service.ProducerService;
import com.itbaizhan.openhis.vo.DataGridView;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

/**
* @author Administrator
* @description 针对表【stock_producer(生产厂家表)】的数据库操作Service实现
* @createDate 2022-08-08 16:20:09
*/
@Service
public class ProducerServiceImpl extends ServiceImpl<ProducerMapper, Producer>
    implements ProducerService{

    @Autowired
    private ProducerMapper producerMapper;

    /**
     * 分页查询生产厂家
     * @param producerDto
     * @return
     */
    @Override
    public DataGridView listProducterForPage(ProducerDto producerDto) {
        Page<Producer> page = new Page<>(producerDto.getPageNum(),producerDto.getPageSize());
        QueryWrapper<Producer> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(producerDto.getProducterName()),Producer.COL_PRODUCTER_NAME,
                producerDto.getProducterName());
        wrapper.like(StringUtils.isNotBlank(producerDto.getKeywords()),Producer.COL_KEYWORDS,
                producerDto.getKeywords());
        wrapper.like(StringUtils.isNotBlank(producerDto.getProducterTel()),Producer.COL_PRODUCTER_TEL,
                producerDto.getProducterTel());
        wrapper.eq(StringUtils.isNotBlank(producerDto.getStatus()),Producer.COL_STATUS,
                producerDto.getStatus());
        wrapper.ge(producerDto.getBeginTime() != null,Producer.COL_CREATE_TIME,
                producerDto.getBeginTime());
        wrapper.le(producerDto.getEndTime() != null,Producer.COL_CREATE_TIME,
                producerDto.getEndTime());
        producerMapper.selectPage(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }


    /**
     * 添加厂家
     * @param producerDto
     * @return
     */
    @Override
    public int addProducter(ProducerDto producerDto) {
        Producer producer = new Producer();
        BeanUtil.copyProperties(producerDto,producer);
        producer.setCreateBy(producerDto.getSimpleUser().getUserName());
        producer.setCreateTime(DateUtil.date());
        return producerMapper.insert(producer);
    }

    /**
     * 修改生产厂家
     * @param producerDto
     * @return
     */
    @Override
    public int updateProducter(ProducerDto producerDto) {
        Producer producer = new Producer();
        BeanUtil.copyProperties(producerDto,producer);
        producer.setUpdateBy(producerDto.getSimpleUser().getUserName());
        producer.setUpdateTime(DateUtil.date());
        return producerMapper.updateById(producer);
    }

    /**
     * 根据id查询厂家
     * @param producterId
     * @return
     */
    @Override
    public Producer getProducterById(Long producterId) {
        return producerMapper.selectById(producterId);
    }

    /**
     * 删除厂家
     * @param producterIds
     * @return
     */
    @Override
    public int deleteProducterByIds(Long[] producterIds) {
        List<Long> ids = Arrays.asList(producterIds);
        if (ids != null && ids.size() > 0){
            return producerMapper.deleteBatchIds(ids);
        }
        return 0;
    }

    @Override
    public List<Producer> selectAllProducter() {
        return producerMapper.selectList(new QueryWrapper<>());
    }
}




